package io.sdl.controller;


import io.sdl.common.constant.UrlConstants;
import io.sdl.common.sys.dto.SdlMessageReadStatusDTO;
import io.sdl.common.sys.service.SdlMessageReadStatusService;
import io.sdl.common.sys.vo.SdlUnMsgVo;
import io.sdl.entity.SdlDiaryCommentsEntity;
import io.sdl.entity.TokenEntity;
import io.sdl.entity.UserEntity;
import io.sdl.service.SdlDiaryCommentsService;
import io.sdl.service.TokenService;
import io.sdl.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.sdl.common.utils.Result;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 消息已读状态表
 *
 * @author Dchen
 * @email 1211734836@qq.com
 * @date 2025-05-12 09:29:35
 */
@RestController
@RequestMapping("api")
public class ApiSdlMessageReadStatusController {

    @Autowired
    private SdlMessageReadStatusService sdlMessageReadStatusService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SdlDiaryCommentsService sdlDiaryCommentsService;

    @Autowired
    private UserService userService;

    @GetMapping("comment/unreadCount")
    @Operation(summary = "获取未读评论总数")
    public Result getUnreadCount(@Parameter(hidden = true) @RequestParam Map<String, Object> params){
        if (params.get("token") != null && !params.get("token").equals("")) {
            TokenEntity byToken = tokenService.getByToken(params.get("token").toString());

            Integer unreadCount = sdlMessageReadStatusService.getUnreadCount(1, byToken.getUserId());
            return new Result().ok(unreadCount);
        }
        return new Result().error(400, "长时间未操作,请重新登录");
    }

    @GetMapping("unreadComment")
    @Operation(summary = "获取未读评论列表")
    public Result getUnreadComment(@Parameter(hidden = true) @RequestParam Map<String, Object> params){
        if (params.get("token") != null && !params.get("token").equals("")) {
            TokenEntity byToken = tokenService.getByToken(params.get("token").toString());

            List<SdlMessageReadStatusDTO> unreadMessageIds = sdlMessageReadStatusService.getUnreadMessageIds(1, byToken.getUserId());
            ArrayList<SdlUnMsgVo> unMsgList = new ArrayList<>();
            unreadMessageIds.forEach(msg->{
                SdlUnMsgVo unMsg = new SdlUnMsgVo();
                //获取评论详情
                SdlDiaryCommentsEntity sdlDiaryCommentsEntity = sdlDiaryCommentsService.selectById(msg.getMessageId());
                UserEntity user = userService.getUserByUserId(sdlDiaryCommentsEntity.getUserId());
                //获取父评论
                SdlDiaryCommentsEntity pComments = sdlDiaryCommentsService.selectById(sdlDiaryCommentsEntity.getParentId());

                UserEntity pUser = userService.getUserByUserId(pComments.getUserId());

                unMsg.setAvatarUrl(UrlConstants.getProductionUrl()+user.getAvatarUrl());
                unMsg.setUsername(user.getUsername());
                unMsg.setContent("回复"+ pUser.getUsername()+":"+sdlDiaryCommentsEntity.getContent());
                unMsg.setCreateAt(sdlDiaryCommentsEntity.getCreatedAt());
                unMsgList.add(unMsg);
            });


            return new Result().ok(unMsgList);
        }
        return new Result().error(400, "长时间未操作,请重新登录");
    }


    @PostMapping("messages/markRead")
    @Operation(summary = "获取未读评论列表")
    public Result markRead(@RequestBody String token ){
        if (token != null && !token.equals("")) {
            // 判断并删除开头的 "
            if (token.startsWith("\"")) {
                token = token.substring(1);
            }

            // 判断并删除结尾的 "
            if (token.endsWith("\"")) {
                token = token.substring(0, token.length() - 1);
            }
            TokenEntity byToken = tokenService.getByToken(token);
            //查询未读的评论列表
            List<SdlMessageReadStatusDTO> unreadMessageIds = sdlMessageReadStatusService.getUnreadMessageIds(1, byToken.getUserId());
            //将评论标记为已读
            unreadMessageIds.forEach(unMsg->{
                sdlMessageReadStatusService.markAsRead(unMsg.getMessageId(),1,byToken.getUserId());
            });

            return new Result().ok(unreadMessageIds);
        }
        return new Result().error(400, "长时间未操作,请重新登录");
    }


}
